//
//  ADTabBarController.m
//  ADKit
//
//  Created by 杜 泽旭 on 2017/12/2.
//  Copyright © 2017年 duzexu. All rights reserved.
//

#import "ADTabBarController.h"
#import "ADKitHeaders.h"

@interface ADTabBarController ()<UITabBarControllerDelegate>
@property (nonatomic, strong, readwrite) UITabBarController *tabBarController;
@end

@implementation ADTabBarController

- (void)initialize {
    [super initialize];
    self.tabBarController = [[UITabBarController alloc] init];
    self.tabBarController.delegate = self;
    /// 添加子控制器
    [self.view addSubview:self.tabBarController.view];
    [self addChildViewController:self.tabBarController];
    [self.tabBarController didMoveToParentViewController:self];
    
    /// 初始化所有的子控制器
    NSArray *models = [self viewControllerModels];
    NSMutableArray *vcs = [NSMutableArray array];
    for (int i = 0; i < models.count; i++) {
        ADViewModel *viewModel = models[i];
        Class cls = [viewModel viewControllerClass];
        if ([cls respondsToSelector:@selector(controllerWithViewModel:)]) {
            UIViewController *vc = [cls performSelector:@selector(controllerWithViewModel:) withObject:viewModel];
            [self configViewController:vc itemIndex:i];
            if (![vc isKindOfClass:UINavigationController.class]) {
                vc = [[ADNavigationController alloc] initWithRootViewController:vc];
            }
            [vcs addObject:vc];
        }
    }
    if (vcs.count > 0) {
        NSUInteger index = [self selectedIndex];
        index = index >= vcs.count ? 0 : index;
        self.tabBarController.viewControllers = vcs;
        self.tabBarController.selectedIndex = index;
        [[ADKit navControllerStack] pushNavigationController:vcs[index]];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    [self didSelectViewController:viewController];
    [[ADKit navControllerStack] bringNavigationController:(UINavigationController *)viewController];
}

- (void)didSelectedIndex:(NSUInteger)selectedIndex {
    if (selectedIndex < self.tabBarController.viewControllers.count) {
        [self.tabBarController setSelectedIndex:selectedIndex];
        UIViewController *viewController = self.tabBarController.viewControllers[selectedIndex];
        [self didSelectViewController:viewController];
        [[ADKit navControllerStack] bringNavigationController:(UINavigationController *)viewController];
    }
}

- (NSArray *)viewControllerModels { return nil; }

- (NSUInteger)selectedIndex { return 0; }

- (void)configViewController:(UIViewController *)viewController itemIndex:(NSInteger)index {}

- (void)didSelectViewController:(UIViewController *)viewController {}

#pragma mark - Override
- (BOOL)shouldAutorotate {
    if ([self.tabBarController.selectedViewController respondsToSelector:@selector(shouldAutorotate)]) {
        return [self.tabBarController.selectedViewController shouldAutorotate];
    }
    return  [super shouldAutorotate];
}

- (UIViewController *)childViewControllerForStatusBarStyle {
    return self.tabBarController.selectedViewController;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    if ([self.tabBarController.selectedViewController respondsToSelector:@selector(supportedInterfaceOrientations)]) {
        return [self.tabBarController.selectedViewController supportedInterfaceOrientations];
    }
    return  [super supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    if ([self.tabBarController.selectedViewController respondsToSelector:@selector(preferredInterfaceOrientationForPresentation)]) {
        return [self.tabBarController.selectedViewController preferredInterfaceOrientationForPresentation];
    }
    return  [super preferredInterfaceOrientationForPresentation];
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    if ([self.tabBarController.selectedViewController respondsToSelector:@selector(preferredStatusBarStyle)]) {
        return self.tabBarController.selectedViewController.preferredStatusBarStyle;
    }
    return [super preferredStatusBarStyle];
}

- (BOOL)prefersStatusBarHidden {
    if ([self.tabBarController.selectedViewController respondsToSelector:@selector(prefersStatusBarHidden)]) {
        return self.tabBarController.selectedViewController.prefersStatusBarHidden;
    }
    return [super prefersStatusBarHidden];
}

@end
